﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace SharePointOfView.Utilities
{
    /// <summary>
    /// Provide a helper class to manage Web.Config modifications programmatically.
    /// <example>
    /// <code>
    /// 
    /// </code>
    /// </example>
    /// </summary>
    public static class WebConfigModification
    {
        /// <summary>
        /// Add a configuration modification to a webapp web.config
        /// </summary>
        /// <param name="webApp">Web Application</param>
        /// <param name="owner">Owner</param>
        /// <param name="modName">Modification's name</param>
        /// <param name="modXPath">Modification's XPath</param>
        /// <param name="modValue">Modification's value</param>
        /// <param name="modType">Modification's Type</param>
        public static void AddConfigModification(SPWebApplication webApp, string owner, string modName, string modXPath, string modValue, SPWebConfigModification.SPWebConfigModificationType modType)
        {
            if (webApp != null)
            {
                SPWebConfigModification modification = new SPWebConfigModification(modName, modXPath);
                modification.Owner = owner;
                modification.Sequence = 0;
                modification.Type = modType;
                modification.Value = modValue;

                // Add the modification.
                webApp.WebConfigModifications.Add(modification);
                // Save web.config changes. 
                webApp.Update();
            }
        }

        /// <summary>
        /// Remove a configuration node
        /// </summary>
        /// <param name="webApp">Web application to apply the modification</param>
        /// <param name="owner">Owner of this modification</param>
        public static void RemoveConfigModification(SPWebApplication webApp, string owner)
        {
            if (webApp != null)
            {
                // Initialize a list to store configuration modifications to remove
                List<SPWebConfigModification> configModsToRemove = new List<SPWebConfigModification>();

                // Remove any modifications that were originally created by the owner.
                foreach (SPWebConfigModification configMod in webApp.WebConfigModifications)
                {
                    if (configMod.Owner == owner)
                    {
                        configModsToRemove.Add(configMod);
                    }
                }

                if (configModsToRemove.Count > 0)
                {
                    foreach (SPWebConfigModification modification in configModsToRemove)
                    {
                        webApp.WebConfigModifications.Remove(modification);
                    }
                    // Save web.config changes. 
                    webApp.Update();
                }
            }
        }

        /// <summary>
        /// Apply modifications on web application
        /// </summary>
        /// <param name="webApp">Specified Web Application</param>
        public static void ApplyModifications(SPWebApplication webApp)
        {
            // Serialize the web application state and propagate changes across the farm. 
            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
        }
    }
}
